/**
 * \brief Initial game mode that will be used for testing purposes
 * This will likely be the default game mode for quite some time, 
 * so I will try to make it as flexible as possible, so if we want
 * to extend this class we can.
 */

using System;
using UnityEngine;
using KaoticSSB.Players;
using KaoticSSB.Online;
using System.Collections.Generic;

namespace KaoticSSB.GameModes
{
	public class GameMode_Basic : GameMode
	{
		public GameMode_Basic() 
		{
			Name = "Vs.";
		}

		public override void Startup ()
		{
			NetworkManager manager = GameObject.FindObjectOfType<NetworkManager>();
			if(manager != null && NetworkManager.Connected)
			{
				manager.GetNetworkView().RPC("CreatePlayer", RPCMode.All, 1);
			}
			else
			{
				CreatePlayer(1);
				CreatePlayer(2);
			}
		}

		public override void Shutdown ()
		{
		}

		public override void GameModeUpdate ()
		{
			foreach(Player player in mPlayers)
			{
				if(player != null && player.Dead)
				{
					if(player.RespawnTimer <= 0.0f)
					{
						player.Respawn();
					}
				}
			}
		}

		public override bool ShouldShutdown ()
		{
			return mFlaggedForShutdown;
		}

		public override void FlagForShutdown ()
		{
			mFlaggedForShutdown = true;
		}

		[RPC]
		private void CreatePlayer(int index)
		{
			mPlayerManager = GameObject.FindObjectOfType<PlayerManager>();
			Rigidbody2D playerObject = mPlayerManager.CreatePlayer(index);
			Player player = playerObject.GetComponent<Player>();
			mPlayers.Add (player);
		}

		private PlayerManager mPlayerManager;
		private List<Player> mPlayers = new List<Player>();
	}
}

